/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 12-Apr-2010, 23:18:00
 */
package fileman;

import NodeManager.Node;
import NodeManager.ManagedFile;
import NodeManager.RdfModelManager;
import FileMaker.MakeFile;
import NodeManager.IRdfViewer;
import NodeManager.NodeManagerService;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author Sean
 */
public class MainGUI extends javax.swing.JFrame implements IRdfViewer {

    RdfModelManager _rdfModelManager;
    File _rdfFile;
    NodeManagerService _nodeManagerService;
    ManagedFile _managedFile;

    /** Creates new form NewJFrame */
    public MainGUI() {

        NodeChooserDialog ncd = new NodeChooserDialog(this, true);

        ncd.setVisible(true);

        if(ncd.isCancelled()){
            System.exit(0);
        }
        
        try {
            //_rdfFile = new File(Global.Data.RdfFile);
            //initNodeManager();
            initComponents();
            Refresh();

        } catch (Exception e) {
            //System.out.println(e.printStackTrace());
            e.printStackTrace();
        }

    }

    public ListModel getFolders() {

        DefaultListModel listModel = new DefaultListModel();
        //ArrayList folders = ViewCreator.getFoldersAsList();
        ArrayList folders = new ArrayList();

        folders.add(_rdfModelManager.GetNode1().NodeName);
        folders.add(_rdfModelManager.GetNode2().NodeName);
        folders.add(_rdfModelManager.GetNode3().NodeName);

        Iterator itr = folders.iterator();
        while (itr.hasNext()) {
            listModel.addElement(itr.next());
        }

        return listModel;

    }

    private void initNodeManager() throws Exception {

        if (!_rdfFile.exists()) {
            Node node1 = new Node(Global.Data.Node1Name, Global.Data.Node1Path);
            Node node2 = new Node(Global.Data.Node2Name, Global.Data.Node2Path);
            Node node3 = new Node(Global.Data.Node3Name, Global.Data.Node3Path);

            _rdfModelManager = new RdfModelManager(node1, node2, node3);
        } else {
            _rdfModelManager = new RdfModelManager(_rdfFile);
        }

        _nodeManagerService = new NodeManagerService(this._rdfModelManager, this);
        _nodeManagerService.start();

    }

    public void Message(String message) {
        if (txtAvailableNodes != null) {
            txtAvailableNodes.setText(message);
        }
    }

    public void Refresh() {
        String filter = "";

        int selectedRow = jTable1.getSelectedRow();

        if (txtFilter != null) {
            filter = txtFilter.getText();
        }

        Vector<ManagedFile> files;

        if (filter.trim().length() == 0) {
            files = _rdfModelManager.GetAllManagedFiles();
        } else {
            // Get all files that are stored around the 3 nodes.
            files = _rdfModelManager.FindFiles(filter);
        }

        TableModel tableModel = new DefaultTableModel(columnNames, files.size());

        int rowcount = 0;
        for (ManagedFile nodeFile : files) {
            tableModel.setValueAt(nodeFile.FileName, rowcount, 0);
            tableModel.setValueAt(nodeFile.FirstNode.NodeName, rowcount, 1);
            if (nodeFile.SecondNode != null) {
                tableModel.setValueAt(nodeFile.SecondNode.NodeName, rowcount, 2);
            }
            rowcount++;
        }

        jTable1.setModel(tableModel);

        try {
            jTable1.setRowSelectionInterval(selectedRow, selectedRow);
        } catch (Exception e) {
        }

    }

    @Action
    public void ViewOptions() {
        Options opt = new Options(this, true);
        opt.setVisible(true);
    }

    @org.jdesktop.application.Action
    public void clearFiles() throws Exception {

        try {

            _nodeManagerService.stopService();
            _managedFile = null;
            for (ManagedFile managedFile : _rdfModelManager.GetAllManagedFiles()) {
                _rdfModelManager.DeleteFile(managedFile.FileName);
            }
            this.Refresh();
            foldersList1.setSelectedIndex(0);
            txtFileContents.setText("");
            DefaultListModel model = (DefaultListModel) filesList1.getModel();
            model.clear();
            
        } catch (Exception e) {
            e.printStackTrace(System.out);
        } finally {
            _nodeManagerService.StartService();
        }

    }

    @Action
    public void AddRandomFiles() {
        // Create some random files, and save them in a temp folder.
        MakeFile make = new MakeFile();
        Vector<File> files;
        try {
            files = make.CreateRandomFiles(Global.Data.NodesDirectory + File.separatorChar + "Temp", 1);
            for (File file : files) {
                // Using the NodeManager, add each file to 2 out of the 3 nodes:
                _rdfModelManager.AddFile(file, true);
                file.delete();
            }
        } catch (Exception ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        Refresh();
        foldersList1.setSelectedIndex(0);
        RefreshSelectedNode();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        foldersList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        filesList1 = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtFileContents = new javax.swing.JTextPane();
        txtFilter = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtAvailableNodes = new javax.swing.JTextField();
        cmdSave = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        cmdDelete = new javax.swing.JButton();
        cmdOpen = new javax.swing.JButton();
        cmdAddFile = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 300));
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Filename", "1st Node", "2nd Node"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTable1MouseReleased(evt);
            }
        });
        jScrollPane5.setViewportView(jTable1);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        foldersList1.setModel(getFolders());
        foldersList1.setName("foldersList1"); // NOI18N
        foldersList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                foldersList1selectList1(evt);
            }
        });
        jScrollPane1.setViewportView(foldersList1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        filesList1.setModel(ViewCreator.getFiles(_rdfModelManager, foldersList1.getSelectedValue()));
        filesList1.setName("filesList1"); // NOI18N
        jScrollPane2.setViewportView(filesList1);

        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(fileman.FilemanApp.class).getContext().getResourceMap(MainGUI.class);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        txtFileContents.setName("txtFileContents"); // NOI18N
        txtFileContents.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtFileContentsKeyReleased(evt);
            }
        });
        jScrollPane3.setViewportView(txtFileContents);

        txtFilter.setText(resourceMap.getString("txtFilter.text")); // NOI18N
        txtFilter.setName("txtFilter"); // NOI18N
        txtFilter.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtFilterKeyReleased(evt);
            }
        });

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        txtAvailableNodes.setEditable(false);
        txtAvailableNodes.setText(resourceMap.getString("txtAvailableNodes.text")); // NOI18N
        txtAvailableNodes.setName("txtAvailableNodes"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(fileman.FilemanApp.class).getContext().getActionMap(MainGUI.class, this);
        cmdSave.setAction(actionMap.get("SaveFile")); // NOI18N
        cmdSave.setText(resourceMap.getString("cmdSave.text")); // NOI18N
        cmdSave.setEnabled(false);
        cmdSave.setName("cmdSave"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(201, 201, 201)
                .addComponent(jLabel2)
                .addGap(6, 6, 6)
                .addComponent(jLabel3)
                .addGap(442, 442, 442))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(jLabel5)
                        .addGap(106, 106, 106))
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                    .addComponent(cmdSave))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtAvailableNodes, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel6)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addGap(1, 1, 1)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSave)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtAvailableNodes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setAction(actionMap.get("AddRandomFiles")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("clearFiles")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        cmdDelete.setAction(actionMap.get("DeleteFile")); // NOI18N
        cmdDelete.setText(resourceMap.getString("cmdDelete.text")); // NOI18N
        cmdDelete.setName("cmdDelete"); // NOI18N

        cmdOpen.setAction(actionMap.get("OpenFile")); // NOI18N
        cmdOpen.setText(resourceMap.getString("cmdOpen.text")); // NOI18N
        cmdOpen.setName("cmdOpen"); // NOI18N

        cmdAddFile.setAction(actionMap.get("AddFile")); // NOI18N
        cmdAddFile.setText(resourceMap.getString("cmdAddFile.text")); // NOI18N
        cmdAddFile.setName("cmdAddFile"); // NOI18N

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setAction(actionMap.get("CloseApp")); // NOI18N
        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setAction(actionMap.get("ViewOptions")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAction(actionMap.get("CloseApp")); // NOI18N
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdOpen, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdAddFile, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                        .addComponent(cmdDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdAddFile, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                        .addComponent(cmdOpen, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-726)/2, (screenSize.height-539)/2, 726, 539);
    }// </editor-fold>//GEN-END:initComponents

    private void RefreshSelectedNode() {
        filesList1.setModel(ViewCreator.getFiles(_rdfModelManager, foldersList1.getSelectedValue()));
    }

    private void foldersList1selectList1(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_foldersList1selectList1
        RefreshSelectedNode();
}//GEN-LAST:event_foldersList1selectList1

    private void jTable1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseReleased
        OpenFile();
    }//GEN-LAST:event_jTable1MouseReleased

    private void txtFilterKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFilterKeyReleased
        Refresh();
    }//GEN-LAST:event_txtFilterKeyReleased

    private void txtFileContentsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtFileContentsKeyReleased
        if (_managedFile != null) {
            cmdSave.setEnabled(true);
        }
    }//GEN-LAST:event_txtFileContentsKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainGUI().setVisible(true);
            }
        });
    }

    @Action
    public void CloseApp() {
        _nodeManagerService.stopService();
        System.exit(0);
    }

    @Action
    public void DeleteFile() {

        try {
            _managedFile = null;

            _nodeManagerService.stopService();

            Thread.sleep(5000);

            int selectedRow = jTable1.getSelectedRow();
            if (selectedRow == -1) {
                return;
            } else {
                String fileName = jTable1.getValueAt(selectedRow, 0).toString();
                _rdfModelManager.DeleteFile(fileName);
                RefreshSelectedNode();
                Refresh();
            }
            txtFileContents.setText("");

        } catch (Exception e) {
            e.printStackTrace(System.out);
        } finally {
            _nodeManagerService.StartService();
        }
    }

    @Action
    public void OpenFile() {
        int selectedRow = jTable1.getSelectedRow();
        if (selectedRow == -1) {
            return;
        } else {

            String fileName = jTable1.getValueAt(selectedRow, 0).toString();
            _managedFile = _rdfModelManager.GetManagedFile(fileName);
            File file = _rdfModelManager.GetPhysicalFile(fileName);
            txtFileContents.setText(ReadFile(file.getPath()));

        }
    }

    public String ReadFile(String fileName) {

        //Open the file for reading
        String thisLine;
        String contents = "";

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(fileName));
            while ((thisLine = br.readLine()) != null) { // while loop begins here
                contents += thisLine + "\n";
            } // end while
            br.close();
        } // end try
        catch (IOException e) {
            System.err.println("Error: " + e);
        }
       

        return contents;

    }

    @Action
    public void AddFile() {
        JFileChooser jfc = new JFileChooser();
        int result = jfc.showOpenDialog(this);
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        }
        try {
            File file = jfc.getSelectedFile();
            _rdfModelManager.AddFile(file, rootPaneCheckingEnabled);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage(),
                    "File error", JOptionPane.ERROR_MESSAGE);
        }
    }

    @Action
    public void SaveFile() {
        try {

            if (_managedFile == null) {
                return;
            }

            _nodeManagerService.stopService();
            File file = _rdfModelManager.GetPhysicalFile(_managedFile.FileName);
            String text = txtFileContents.getText();
            MakeFile.UpdateFile(file, txtFileContents.getText());
            cmdSave.setEnabled(false);

        } catch (Exception e) {
        } finally {
            _nodeManagerService.StartService();
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAddFile;
    private javax.swing.JButton cmdDelete;
    private javax.swing.JButton cmdOpen;
    private javax.swing.JButton cmdSave;
    private javax.swing.JList filesList1;
    private javax.swing.JList foldersList1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtAvailableNodes;
    private javax.swing.JTextPane txtFileContents;
    private javax.swing.JTextField txtFilter;
    // End of variables declaration//GEN-END:variables
    private String[] columnNames = new String[]{
        "Filename", "1st Node", "2nd Node"
    };
}
